package com.haha.qqchannelbot.service;

import com.haha.qqchannelbot.entity.Bot;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Service
@Log4j2
public class BotRestTemplate {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private Bot bot;

    public String exchange(String uri, HttpMethod httpMethod, MultiValueMap<String, Object> params) {
        log.info("guild url:" + bot.getBaseUrl() + uri);
        ResponseEntity<String> responseEntity = restTemplate.exchange(bot.getBaseUrl() + uri,
                httpMethod,
                httpEntity(params),
                String.class);
        if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            return responseEntity.getBody();
        }

        return null;
    }

    public String exchange(String uri, HttpMethod httpMethod) {
        return exchange(uri, httpMethod, new LinkedMultiValueMap<>());
    }

    public String postForBodyJson(String uri, String bodyJson) {
        log.info("guild url:" + bot.getBaseUrl() + uri);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(bot.getBaseUrl() + uri,
                bodyHttpEntity(bodyJson),
                String.class);
        if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            return responseEntity.getBody();
        }

        return null;
    }

    public String uploadFileWithParameters(String uri, File file, String paramName, String paramValue) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.set(HttpHeaders.AUTHORIZATION, bot.getToken());

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file_image", (new FileInputStream(file)).readAllBytes());
        body.add("msg_id", paramValue);
//        body.add("content", "image");

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(bot.getBaseUrl() + uri, requestEntity, String.class);

        // 处理响应
        log.info("Response: " + response.getBody());
        return response.getBody();
    }

    private byte[] fileToByteArray(File file) {
        try {
            return org.apache.commons.io.FileUtils.readFileToByteArray(file);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    private HttpHeaders httpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(HttpHeaders.AUTHORIZATION, bot.getToken());
        return headers;
    }

    private HttpEntity httpEntity(MultiValueMap<String, Object> params) {
        return new HttpEntity(params, httpHeaders());
    }

    private <T> HttpEntity<T> bodyHttpEntity(T body) {
        return new HttpEntity<T>(body, httpHeaders());
    }

}
